package com.haixiaoke.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.mapper.BrandMapper;
import com.haixiaoke.mapper.ModelMapper;
import com.haixiaoke.domain.Model;
import com.haixiaoke.service.IModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 型号Service业务层处理
 *
 * @author zhiping
 * @date 2023-05-24
 */
@Service
public class ModelServiceImpl implements IModelService {
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询型号
     *
     * @param id 型号主键
     * @return 型号
     */
    @Override
    public Model selectModelById(Long id) {
        return modelMapper.selectModelById(id);
    }

    /**
     * 查询型号列表
     *
     * @param model 型号
     * @return 型号
     */
    @Override
    public List<Model> selectModelList(Model model) {
        return modelMapper.selectModelList(model);
    }

    /**
     * 新增型号
     *
     * @param model 型号
     * @return 结果
     */
    @Override
    public int insertModel(Model model) {
        //查询数据中是否存在此型号
        Model sqlModel = modelMapper.selectModel(model);
        if (ObjectUtil.isNotEmpty(sqlModel)) {
            throw new ServiceException("型号已经存在");
        }
        return modelMapper.insertModel(model);
    }

    /**
     * 修改型号
     *
     * @param model 型号
     * @return 结果
     */
    @Override
    public int updateModel(Model model) {
        return modelMapper.updateModel(model);
    }

    /**
     * 批量删除型号
     *
     * @param ids 需要删除的型号主键
     * @return 结果
     */
    @Override
    public int deleteModelByIds(Long[] ids) {
        return modelMapper.deleteModelByIds(ids);
    }

    /**
     * 删除型号信息
     *
     * @param id 型号主键
     * @return 结果
     */
    @Override
    public int deleteModelById(Long id) {
        return modelMapper.deleteModelById(id);
    }

}
